<?php

class Quad_Curl_Response {
	
	protected $_request;
	protected $_result;
	protected $_errorNo = 0;
	protected $_errorString = '';
	protected $_info = array();
	
	public function __construct(Quad_Curl $request, $result, $handle) {
		$this->_request = $request;
		$this->_result = $result;
		$this->_errorNo = curl_errno($handle);
		$this->_errorString = curl_error($handle);
		$this->_info = curl_getinfo($handle);
		if ($this->_info['download_content_length'] == -1 && $this->_errorNo === false) {
			$this->_errorNo = -1;
			$this->_errorString = 'Server sent empty response. Check URL.';
		} 
	}
	
	public function getRequest() {
		return $this->_request;
	}
	
	public function getResult() {
		return $this->_result;
	}
	
	public function __toString() {
		return (string) $this->_result;
	}
	
	// getInfo() Types: http://php.net/curl_getinfo
	//
	// url
	// content_type
	// http_code
	// header_size
	// request_size
	// filetime
	// ssl_verify_result
	// redirect_count
	// total_time
	// namelookup_time
	// connect_time
	// pretransfer_time
	// size_upload
	// size_download
	// speed_download
	// speed_upload
	// download_content_length
	// upload_content_length
	// starttransfer_time
	// redirect_time
	public function getInfo($key = null) {
		if ($key) {
			return $this->_info[$key];
		}
		return $this->_info;
	}
	
	public function hasError() {
		return $this->_errorNo > 0;
	}
	
	public function error() {
		return $this->_errorString;
	}
	
	public function getErrorString() {
		return $this->_errorString;
	}
	
	public function errno() {
		return $this->_errorNo;
	}
	
	public function getErrorNo() {
		return $this->_errorNo;
	}
	
	public function getTotalTime() {
		return $this->getInfo('total_time');
	}
			
	public function getHttpStatus() {
		return $this->getInfo('http_code');
	}
	
	public function getHttpStatusText() {
		// from http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
		static $statuses = array(
			// Informational 1xx
			100=>"Continue",
			101=>"Switching Protocols",
			// Successful 2xx
			200=>"OK",
			201=>"Created",
			202=>"Accepted",
			203=>"Non-Authoritative Information",
			204=>"No Content",
			205=>"Reset Content",
			206=>"Partial Content",
			// Redirection 3xx
			300=>"Multiple Choices",
			301=>"Moved Permanently",
			302=>"Found",
			303=>"See Other",
			304=>"Not Modified",
			305=>"Use Proxy",
			306=>"(Unused)",
			307=>"Temporary Redirect",
			// Client Error 4xx
			400=>"Bad Request",
			401=>"Unauthorized",
			402=>"Payment Required",
			403=>"Forbidden",
			404=>"Not Found",
			405=>"Method Not Allowed",
			406=>"Not Acceptable",
			407=>"Proxy Authentication Required",
			408=>"Request Timeout",
			409=>"Conflict",
			410=>"Gone",
			411=>"Length Required",
			412=>"Precondition Failed",
			413=>"Request Entity Too Large",
			414=>"Request-URI Too Long",
			415=>"Unsupported Media Type",
			416=>"Requested Range Not Satisfiable",
			417=>"Expectation Failed",
			// Server Error 5xx
			500=>"Internal Server Error",
			501=>"Not Implemented",
			502=>"Bad Gateway",
			503=>"Service Unavailable",
			504=>"Gateway Timeout",
			505=>"HTTP Version Not Supported",
		);
		$code = $this->getHttpStatus();
		return $statuses[$code];
	}
	
	# super long list of constants that are alternates of CURL* constants 
	// http://php.net/manual/en/curl.constants.php accessed as of PHP 5.2.11
	const INFO_PRIVATE  = CURLINFO_PRIVATE ;
	// ^ Available since PHP 5.2.4 
	
	const INFO_EFFECTIVE_URL  = CURLINFO_EFFECTIVE_URL ;
	
	const INFO_HTTP_CODE  = CURLINFO_HTTP_CODE ;
	
	const INFO_HEADER_OUT  = CURLINFO_HEADER_OUT ;
	// ^ Available since PHP 5.1.3 
	
	const INFO_HEADER_SIZE  = CURLINFO_HEADER_SIZE ;
	
	const INFO_REQUEST_SIZE  = CURLINFO_REQUEST_SIZE ;
	
	const INFO_TOTAL_TIME  = CURLINFO_TOTAL_TIME ;
	
	const INFO_NAMELOOKUP_TIME  = CURLINFO_NAMELOOKUP_TIME ;
	
	const INFO_CONNECT_TIME  = CURLINFO_CONNECT_TIME ;
	
	const INFO_PRETRANSFER_TIME  = CURLINFO_PRETRANSFER_TIME ;
	
	const INFO_SIZE_UPLOAD  = CURLINFO_SIZE_UPLOAD ;
	
	const INFO_SIZE_DOWNLOAD  = CURLINFO_SIZE_DOWNLOAD ;
	
	const INFO_SPEED_DOWNLOAD  = CURLINFO_SPEED_DOWNLOAD ;
	
	const INFO_SPEED_UPLOAD  = CURLINFO_SPEED_UPLOAD ;
	
	const INFO_FILETIME  = CURLINFO_FILETIME ;
	
	const INFO_SSL_VERIFYRESULT  = CURLINFO_SSL_VERIFYRESULT ;
	
	const INFO_CONTENT_LENGTH_DOWNLOAD  = CURLINFO_CONTENT_LENGTH_DOWNLOAD ;
	
	const INFO_CONTENT_LENGTH_UPLOAD  = CURLINFO_CONTENT_LENGTH_UPLOAD ;
	
	const INFO_STARTTRANSFER_TIME  = CURLINFO_STARTTRANSFER_TIME ;
	
	const INFO_CONTENT_TYPE  = CURLINFO_CONTENT_TYPE ;
	
	const INFO_REDIRECT_TIME  = CURLINFO_REDIRECT_TIME ;
	
	const INFO_REDIRECT_COUNT  = CURLINFO_REDIRECT_COUNT ;
			
}